<template>
    <div>
        <div class="operate center-flex-box" v-if="hasPermission('PAGE_ROLE_PERMISSION')">
            <el-button class="item" type="primary" v-if="hasPermission('ADD_ROLE_PERMISSION')"
                       @click="showEditor()">
                新增
            </el-button>
            <el-input placeholder="请输入内容" v-model="queryInfo.roleIds" class="input-with-select item">
                <template slot="prepend">角色ID</template>
            </el-input>
            <el-button class="item" @click="obtainRolePermissions">查找</el-button>
        </div>
        <el-table
            v-if="hasPermission('PAGE_ROLE_PERMISSION')"
            :data="rolePermissions"
            stripe
            max-height="532"
            style="width: 100%">
            <el-table-column
                prop="role.id"
                label="角色ID"
                sortable
                width="180">
            </el-table-column>
            <el-table-column
                prop="role.name"
                label="角色名称"
                sortable
                width="160">
            </el-table-column>
            <el-table-column
                prop="permission.id"
                label="权限ID"
                sortable
                width="180">
            </el-table-column>
            <el-table-column
                prop="permission.name"
                sortable
                label="权限名称"
                width="240">
            </el-table-column>
            <el-table-column
                prop="permission.description"
                label="权限描述"
                width="180">
            </el-table-column>
            <el-table-column
                label="可授权"
                width="120">
                <template slot-scope="scope">
                    <el-button @click="update(scope.row.role.id,scope.row.permission.id,false)"
                               v-if="scope.row.authorize"
                               type="success"
                               size="mini"
                               :disabled="!hasPermission('UPDATE_ROLE_PERMISSION')"
                               plain>
                        可授权
                    </el-button>
                    <el-button @click="update(scope.row.role.id,scope.row.permission.id,true)"
                               v-else
                               type="warning"
                               size="mini"
                               :disabled="!hasPermission('UPDATE_ROLE_PERMISSION')"
                               plain>
                        不可授权
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column
                v-if="hasPermission('DELETE_ROLE_PERMISSION')"
                fixed="right"
                width="80"
                label="删除操作">
                <template slot-scope="scope">
                    <el-button @click="remove(scope.row.role.id,scope.row.permission.id)"
                               type="danger"
                               size="mini"
                               plain>
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="新增角色权限" :visible.sync="editor" width="40%">
            <el-form ref="rolePermission" :rules="rules" :model="rolePermission" label-position="left"
                     label-width="120px"
                     size="small">
                <el-form-item label="角色" prop="roleId">
                    <el-select class="left" v-model="rolePermission.roleId" placeholder="请选择角色">
                        <el-option
                            v-for="role in roles"
                            :key="role.id"
                            :label="role.name"
                            :value="role.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="权限" prop="permissionId">
                    <el-select class="left" v-model="rolePermission.permissionId" placeholder="请选择">
                        <el-option
                            v-for="permission in permissions"
                            :key="permission.id"
                            :label="permission.name"
                            :value="permission.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="可授权">
                    <div class="authorize">
                        <el-switch v-model="rolePermission.authorize"></el-switch>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editor = false">取 消</el-button>
                <el-button type="primary" @click="commit">提交</el-button>
            </div>
        </el-dialog>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageInfo.page"
            :page-size="pageInfo.size"
            layout="total, prev, pager, next, jumper"
            :total="pageInfo.total">
        </el-pagination>
    </div>
</template>

<script>
    import { mapGetters, mapActions } from 'vuex'

    export default {
        name: 'rolePermissionManagement',
        data () {
            return {
                rowRolePermissions: [],
                rolePermissions: [],
                rolePermission: { roleId: '', permissionId: '', authorize: false },
                pageInfo: {
                    page: 1,
                    size: 100,
                    total: 0
                },
                queryInfo: {
                    roleIds: ''
                },
                editor: false,
                roles: [],
                permissions: [],
                rules: {
                    roleId: [{ required: true, message: '请选择角色', trigger: 'blur' }],
                    permissionId: [{ required: true, message: '请选择权限', trigger: 'blur' }]
                }
            }
        },
        mounted () {
            this.obtainRolePermissions()
            this.action(this.listRoleInfoAction(), data => {
                this.roles = data.data
            })
            this.action(this.listPermissionAction(), data => {
                this.permissions = data.data
            })
        },
        computed: {
            ...mapGetters({
                hasPermission: 'user/hasPermission'
            })
        },
        methods: {
            ...mapActions({
                listRoleInfoAction: 'user/listRoleInfo',
                listPermissionAction: 'user/listPermission',
                pageRolePermissionAction: 'user/pageRolePermission',
                insertRolePermissionAction: 'user/insertRolePermission',
                updateRolePermissionAction: 'user/updateRolePermission',
                deleteRolePermissionAction: 'user/deleteRolePermission'
            }),
            handleSizeChange (val) {
                this.pageInfo.size = val
                this.obtainRolePermissions()
            },
            handleCurrentChange (val) {
                this.pageInfo.page = val
                this.obtainRolePermissions()
            },
            obtainRolePermissions () {
                this.action(this.pageRolePermissionAction(this.obtainQuery()), data => {
                    if (data.data) {
                        this.rowRolePermissions = data.data.records
                        let rolePermissions = data.data.records
                        this.rolePermissions = []
                        rolePermissions.forEach(rolePermission => {
                            rolePermission.permissions.forEach(item => {
                                let temp = {
                                    role: {
                                        ...rolePermission.role
                                    },
                                    authorize: item.authorize,
                                    permission: {
                                        ...item
                                    }
                                }
                                delete temp.permissions
                                this.rolePermissions.push(temp)
                            })
                        })
                        this.pageInfo.total = data.data.total
                    } else {
                        this.rolePermissions = []
                    }
                })
            },
            obtainQuery () {
                let query = {
                    page: this.pageInfo.page,
                    size: this.pageInfo.size,
                    roleIds: this.queryInfo.roleIds
                }
                return query
            },
            showEditor (roleId, permissionId) {
                this.rolePermission = { roleId: '', permissionId: '', authorize: false }
                this.editor = true
            },
            commit () {
                this.$refs['rolePermission'].validate((valid) => {
                    if (valid) {
                        console.log(this.rolePermission)
                        this.action(this.insertRolePermissionAction(this.rolePermission), data => {
                            this.$message.success('添加成功')
                            this.editor = false
                            this.obtainRolePermissions()
                        })
                    } else {
                        return false
                    }
                })
            },
            update (roleId, permissionId, authorize) {
                this.action(this.updateRolePermissionAction({ roleId, permissionId, authorize }), data => {
                    this.$message.success('更新成功')
                    this.rolePermissions.find(rolePermission => rolePermission.role.id === roleId &&
                        rolePermission.permission.id === permissionId).authorize = authorize
                })
            },
            remove (roleId, permissionId) {
                this.action(this.deleteRolePermissionAction({ roleId, permissionId }), data => {
                    this.$message.success('删除成功')
                    this.obtainRolePermissions()
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import '../../../../../style/scss';

    .operate {
        height: 80px;
        width: 96%;
        padding: 0 2%;

        .item {
            margin: 6px;
        }

        .input-with-select {
            width: 360px;
            background-color: #fff;

            .el-select {
                width: 100px;
            }
        }
    }

    .authorize {
        width: 15%;
    }

    .left {
        float: left;
        width: 320px;
    }
</style>
